package com.frxuexi.demo01_compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.frxuexi.demo01_compose.ui.theme.Demo01_ComposeTheme
import com.frxuexi.demo01_compose.view.MainPage
import com.frxuexi.demo01_compose.view.MyCornerDialog
import com.frxuexi.demo01_compose.view.MyDialog
import com.frxuexi.demo01_compose.view.MyGrid
import com.frxuexi.demo01_compose.view.myBadge
import com.frxuexi.demo01_compose.view.myBar
import com.frxuexi.demo01_compose.view.myButton
import com.frxuexi.demo01_compose.view.myCom2
import com.frxuexi.demo01_compose.view.myLogin
import com.frxuexi.demo01_compose.view.mySwitch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Demo01_ComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
//                    Greeting("Android")
                    /* val list = mutableListOf<Message>()
                     for (i in 1..20){
                         list.add(Message(R.drawable.ic_launcher_background,"张三"+i,"描述内容"+i))
                     }
//                     MessageList(list = list)
                    MyGrid(list = list)*/

                    /*myButton(){
                        Log.i("===","登录点击了")
                    }*/

                    /*myCom2(text = "text") {
                        Log.i("===","点击")
                    }*/

                    var openAlertDialog = remember { mutableStateOf(false) }
                    /*MyDialog(openAlertDialog,onDismissRequest = {
                        openAlertDialog.value = false
                        Log.i("===", "关闭")
                    }) {
                        openAlertDialog.value = false
                        Log.i("===", "确认")
                    }*/

                    /*MyCornerDialog(openAlertDialog,onDismissRequest = {
                        openAlertDialog.value = false
                        Log.i("===", "关闭")
                    }) {
                        openAlertDialog.value = false
                        Log.i("===", "确认")
                    }*/
                    /*when {
                        // ...
                        openAlertDialog.value -> {
                            MyDialog(openAlertDialog,onDismissRequest = {
                                openAlertDialog.value = false
                                Log.i("===", "关闭")
                            }) {
                                openAlertDialog.value = false
                                Log.i("===", "确认")
                            }
                        }
                    }*/

//                    myBar()
//                    mySwitch()
//                    myBadge()

//                    myLayout()

                    /*val items = listOf(
                        Screen.Login,
                        Screen.MyCom,
                    )*/

                    val navController = rememberNavController()
//                    startDestination:起始加载页面
                    NavHost(navController = navController, startDestination = "login") {
//                        组件，route:路径名，myLayout：组件名（页面）
                        composable("login") {
                            myLogin(navController)
                        }
                        composable("main/{name}?pass={pass}",
//                            指定参数类型，默认是string
                            arguments = listOf(navArgument("pass"){type= NavType.IntType;defaultValue=0})
                        ) {
//                            取参数
                            val name = it.arguments?.getString("name")
                            val pass = it.arguments?.getInt("pass")
                            MainPage(name!!,pass!!)
                        }
                    }
                }
            }
        }
    }
}


data class Message(val img: Int, val title: String, val content: String)

@Composable
fun MessageItem(message: Message, modifier: Modifier = Modifier) {
    //水平排列
    Row(Modifier.padding(vertical = 5.dp, horizontal = 10.dp)) {
        Image(
            painter = painterResource(id = message.img),
            contentDescription = "",
            modifier = Modifier
                .size(60.dp)
                .clip(CircleShape)
//                    modifier = Modifier.size(60.dp).clip(RoundedCornerShape(20.dp,30.dp,40.dp,50.dp))
        )
        //宽度为10dp的空白区域
        Spacer(modifier = Modifier.width(10.dp))
        //垂直排列
        Column {
            //TextView
            Text(
                text = message.title,
                modifier = modifier
            )
            //高度为10dp的空白区域
            Spacer(modifier = Modifier.height(10.dp))
            Text(text = message.content)
        }

    }

}

@Composable
fun MessageList(list: List<Message>) {
    LazyColumn {
        items(list) { message ->
            MessageItem(message)
        }
    }
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Demo01_ComposeTheme {
        val list = mutableListOf<Message>()
        for (i in 1..20){
            list.add(Message(R.drawable.ic_launcher_background,"张三"+i,"描述内容"+i))
        }
//        MessageList(list = list)
        MyGrid(list = list)
//        Greeting("Compose")

        /*myButton(){

        }*/
//        myCom2()

    }
}